import axios from 'axios'

function genWms(tiffPath) {

  const storeName = tiffPath.split('/').pop().split(".")[0];

  // GeoServer 配置
const GEOSERVER = {
  baseURL: '/geoserver/rest',
  auth: {
    username: 'admin',
    password: 'geoserver'
  },
  workspace: 'test_tif',  // 自定义工作区名称
  storeName: storeName,  // 存储名称
  filePath: `file:///${tiffPath}`, // 支持 file/http/ftp 协议
  crs: 'EPSG:4326'           // 目标坐标系
};


// 创建 HTTP 客户端
const api = axios.create({
  baseURL: GEOSERVER.baseURL,
  auth: GEOSERVER.auth,
  headers: {'Content-Type': 'application/json'}
});

publishByPath()

async function publishByPath() {
  try {
    // 1. 创建工作区
    // await api.post('/workspaces', { 
    //   workspace: { name: GEOSERVER.workspace }
    // }, { validateStatus: status => status < 400 });

    // 2. 创建外部关联的 CoverageStore
    await api.put(`/workspaces/${GEOSERVER.workspace}/coveragestores`, {
      coverageStore: {
        name: GEOSERVER.storeName,
        type: 'GeoTIFF',
        enabled: true,
        url: GEOSERVER.filePath  // 关键参数：指定文件路径
      }
    });

    // 3. 设置坐标系
    await api.put(
      `/workspaces/${GEOSERVER.workspace}/coveragestores/${GEOSERVER.storeName}/coverages/${GEOSERVER.storeName}`,
      { coverage: { srs: GEOSERVER.crs } }
    );

    console.log('通过路径发布成功');
  } catch (error) {
    console.error('错误:', error.response?.data || error.message);
  }
}
  
}

export { genWms }